const api = require("../../../../ws_micro_shop/utils/api.js");
const app = getApp();

Page({
  data: {
    userlist: [],
    page: 1,
    more: true,
    keyword: "",
    currVal: "",
    isEmpty: true,
    showModel: false,
    showDetail: false,
    showEdit: false,
    showRecharge: false,
    rechargenumber: "",
    levelname: "",
    level: "",
    realname: "",
    modeltitle: "",
    rechangetype: ""
  },
  onLoad(options) {
    this.onLoadUserList();
    this.setData({
      isBrand:
        app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isBrand //是否为品牌版
    });
  },
  onReachBottom() {
    this.onLoadUserList();
  },
  onLoadUserList() {
    let that = this;
    let { userlist, page, more, keyword } = that.data;
    if (more) {
      wx.showLoading({
        title: "加载中...",
        mask: true
      });
      app.ws_micro_shoprequest({
        url: api.adminer.userlist,
        data: {
          page,
          keyword
        },
        success: function(res) {
          wx.hideLoading();
          if (res.code === 0) {
            let { list, level_list } = res.data;
            if (list.length > 0) {
              page += 1;
              that.setData({
                userlist: userlist.concat(list),
                level_list,
                page
              });
            } else {
              // wx.showToast({
              //   title: '没有更多数据了',
              //   icon: 'none',
              // });
              that.setData({
                more: false
              });
            }
          } else {
            wx.showToast({
              title: res.msg || "加载出错",
              icon: "none"
            });
          }
        }
      });
    }
  },
  clearTxt() {
    this.setData({
      currVal: "",
      keyword: "",
      page: 1,
      userlist: [],
      more: true,
      isEmpty: true
    });
    this.onLoadUserList();
  },
  inputChange(e) {
    let val = e.detail.value;
    this.setData({
      isEmpty: !val.trim(),
      currVal: val
    });
  },
  inputConfirm(e) {
    let keyword = e.detail.value.trim();
    this.setData({
      page: 1,
      keyword,
      userlist: [],
      more: true
    });
    this.onLoadUserList();
  },
  searchTxt(keyword) {
    this.setData({
      page: 1,
      keyword,
      userlist: [],
      more: true
    });
    this.onLoadUserList();
  },
  searchSure() {
    let keyword = this.data.currVal.trim();
    this.searchTxt(keyword);
  },
  openPop(e) {
    let id = Number(e.currentTarget.dataset.uid);
    let userlist = this.data.userlist;
    let len = userlist.length;
    for (let i = 0; i < len; i++) {
      if (Number(userlist[i].id) === id) {
        userlist[i].isShow = true;
      } else {
        userlist[i].isShow = false;
      }
    }
    this.setData({
      userlist,
      uid: id
    });
  },
  closePop(e) {
    let id = Number(e.currentTarget.dataset.uid);
    let userlist = this.data.userlist;
    let len = userlist.length;
    for (let i = 0; i < len; i++) {
      if (Number(userlist[i].id) === id) {
        userlist[i].isShow = false;
        break;
      }
    }
    this.setData({
      userlist
    });
  },
  userOpr(e) {
    let that = this;
    let { type, index } = e.detail;
    let { userlist, uid } = this.data;
    if (type === "save_admin") {
      wx.showModal({
        title: "提示",
        content: "设置管理员后即可在小程序端管理商家后台，是否确认操作？",
        success: res => {
          if (res.confirm) {
            app.ws_micro_shoprequest({
              url: api.adminer.setadmin,
              method: "POST",
              data: {
                id: uid,
                del: 2
              },
              success: resp => {
                if (resp.code === 0) {
                  wx.showToast({
                    title: "设置成功"
                  });
                  userlist[index].is_admin = 1;
                  that.setData({
                    userlist
                  });
                } else {
                  wx.showToast({
                    title: "设置失败",
                    icon: "none"
                  });
                }
              }
            });
          }
        }
      });
    } else if (type === "cancel_admin") {
      wx.showModal({
        title: "提示",
        content: "取消管理员后即不可在小程序端管理商家后台，是否确认操作？",
        success: res => {
          if (res.confirm) {
            app.ws_micro_shoprequest({
              url: api.adminer.setadmin,
              method: "POST",
              data: {
                id: uid,
                del: 1
              },
              success: resp => {
                if (resp.code === 0) {
                  wx.showToast({
                    title: "取消成功"
                  });
                  userlist[index].is_admin = 0;
                  that.setData({
                    userlist
                  });
                } else {
                  wx.showToast({
                    title: "取消失败",
                    icon: "none"
                  });
                }
              }
            });
          }
        }
      });
    } else if (type === "add_black") {
      wx.showModal({
        title: "提示",
        content: "是否确定加入黑名单？",
        success: res => {
          if (res.confirm) {
            app.ws_micro_shoprequest({
              url: api.adminer.blacklist,
              method: "POST",
              data: {
                id: uid,
                blacklist: 1
              },
              success: resp => {
                if (resp.code === 0) {
                  wx.showToast({
                    title: "加入成功"
                  });
                  userlist[index].blacklist = 1;
                  that.setData({
                    userlist
                  });
                } else {
                  wx.showToast({
                    title: "加入失败",
                    icon: "none"
                  });
                }
              }
            });
          }
        }
      });
    } else if (type === "cancel_black") {
      wx.showModal({
        title: "提示",
        content: "是否确定取消黑名单？",
        success: res => {
          if (res.confirm) {
            app.ws_micro_shoprequest({
              url: api.adminer.blacklist,
              method: "POST",
              data: {
                id: uid,
                blacklist: 0
              },
              success: resp => {
                if (resp.code === 0) {
                  wx.showToast({
                    title: "取消成功"
                  });
                  userlist[index].blacklist = 0;
                  that.setData({
                    userlist
                  });
                } else {
                  wx.showToast({
                    title: "取消失败",
                    icon: "none"
                  });
                }
              }
            });
          }
        }
      });
    } else if (type === "detail") {
      that.onLoadUserDetail(function() {
        that.setData({
          showDetail: true
        });
      });
    } else if (type === "edit") {
      that.onLoadUserDetail(function() {
        that.setData({
          showEdit: true
        });
      });
    } else if (type === "integral") {
      that.setData({
        showModel: true,
        showRecharge: true,
        modeltitle: "积分设置"
      });
    } else if (type === "balance") {
      that.setData({
        showModel: true,
        showRecharge: true,
        modeltitle: "余额设置"
      });
    }
    that.setData({
      index,
      type
    });
  },
  onLoadUserDetail(cb) {
    let that = this;
    let { uid } = this.data;
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    app.ws_micro_shoprequest({
      url: api.adminer.userinfo,
      method: "POST",
      data: {
        id: uid
      },
      success: resp => {
        wx.hideLoading();
        if (resp.code === 0) {
          let userObj = resp.data;
          let { level, realname } = userObj.user;
          that.setData({
            userObj,
            showModel: true,
            level,
            realname
          });
          cb();
        } else {
          wx.showToast({
            title: resp.msg || "获取失败",
            icon: "none"
          });
        }
      }
    });
  },
  closeModel() {
    this.setData({
      showModel: false,
      showDetail: false,
      showEdit: false,
      showRecharge: false,
      level: "",
      levelname: "",
      rechargenumber: ""
    });
  },
  chooseMemberLevel(e) {
    let { level_list } = this.data;
    let idx = e.detail.value;
    this.setData({
      levelname: level_list[idx].name,
      level: level_list[idx].level
    });
  },
  getRealName(e) {
    this.setData({
      realname: e.detail.value.trim()
    });
  },
  getRechargeNumber(e) {
    this.setData({
      rechargenumber: e.detail.value.trim()
    });
  },
  saveEdit() {
    let that = this;
    let { uid, userlist, index, level, levelname, realname } = that.data;
    app.ws_micro_shoprequest({
      url: api.adminer.useredit,
      method: "POST",
      data: {
        id: uid,
        level,
        realname
      },
      success: res => {
        if (res.code === 0) {
          userlist[index].l_name = res.data.levelname;
          userlist[index].level = res.data.level;
          userlist[index].realname = res.data.realname;
          that.setData({
            userlist,
            showModel: false,
            showEdit: false,
            level: "",
            levelname: ""
          });
          wx.showToast({
            title: "保存成功"
          });
        } else {
          wx.showToast({
            title: res.msg || "保存失败",
            icon: "none"
          });
        }
      }
    });
  },
  chooseRechangeType(e) {
    console.log(e);
    this.setData({
      rechangetype: e.detail.value
    });
  },
  submitRecharge() {
    let that = this;
    let {
      uid,
      userlist,
      index,
      type,
      rechangetype,
      rechargenumber
    } = that.data;
    app.ws_micro_shoprequest({
      url: api.adminer.recharge,
      method: "POST",
      data: {
        id: uid,
        [type]: rechargenumber,
        rechangeType: rechangetype
      },
      success: res => {
        if (res.code === 0) {
          type === "integral" &&
            (userlist[index].total_integral = res.data.integral);
          type === "balance" && (userlist[index].money = res.data.money);
          that.setData({
            userlist,
            showModel: false,
            showRecharge: false,
            rechargenumber: ""
          });
          wx.showToast({
            title: Number(rechangetype) === 1 ? "充值成功" : "扣除成功"
          });
        } else {
          wx.showToast({
            title:
              res.msg || Number(rechangetype) === 1 ? "充值失败" : "扣除失败",
            icon: "none"
          });
        }
      }
    });
  }
});
